package com.example.lenovo.manager;

import android.app.Dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;

import java.util.List;

public class RegisterPage extends AppCompatActivity {

    private EditText UserNameRegister;
    private EditText PassWordRegister;
    private EditText PassWordResure;
    private EditText MobileRegister;
    private EditText RealNameRegister;
    private TextView Error;
    private Button Register;
    private boolean Flag;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_page);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        AVOSCloud.initialize(this, "Po6OudEc9QxRgv1qcussxBxi-gzGzoHsz", "EFNsW7y5aXtKMkpU50xyhHNk");

        /*连接组件*/
        UserNameRegister = (EditText) findViewById(R.id.UserNameRegister);
        PassWordRegister = (EditText) findViewById(R.id.PassWordRegister);
        PassWordResure = (EditText) findViewById(R.id.PassWordResure);
        MobileRegister = (EditText) findViewById(R.id.MobileRegister);
        RealNameRegister = (EditText) findViewById(R.id.RealNameRegister);
        Error = (TextView) findViewById(R.id.Error);
        Register = (Button) findViewById(R.id.CommitRegister);
        /*设置监听器*/
        Register.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(CheckedInfo() ){
                            RegisterOperat();
                        }
                    }
                }
        );

    }

    /**
     * 验证信息完整性与正确性
     * @return
     */
    private boolean CheckedInfo(){

        /*用户名非空验证*/
        if(UserNameRegister.getText().toString().length() == 0){
            Error.setText("*用户名不能为空！");
            /*UserNameRegister.setFocusable(true);*/
            return false;
        }
        /*用户密码非空验证*/
        if(PassWordRegister.getText().toString().length() == 0){
            Error.setText("*密码不能为空");
            /*PassWordRegister.setFocusable(true);*/
            return false;
        }
        /*用户密码位数验证*/
        if(PassWordRegister.getText().toString().length() < 6){
            Error.setText("*密码位数至少为6位！");
            /*PassWordRegister.setFocusable(true);*/
            return false;
        }
        /*用户密码与确认密码一致性验证*/
        if(!(PassWordRegister.getText().toString().equals(PassWordResure.getText().toString()))){
            Error.setText("*密码与确认密码不一致！");
            /*PassWordRegister.setFocusable(true);*/
            return false;
        }
        if(MobileRegister.getText().toString().length()!=11){
            Error.setText("*手机号输入有误！");
            /*MobileRegister.setFocusable(true);*/
            return false;
        }
        if(RealNameRegister.getText().toString().length() == 0){
            Error.setText("*请输入真实姓名！");
            /*RealNameRegister.setFocusable(true);*/
            return false;
        }
        Error.setText("");
        return true;
    }

    private void RegisterOperat(){

        AVQuery<AVObject> query = new AVQuery<>("ManagerUser");
        query.whereEqualTo("UserName", UserNameRegister.getText().toString());
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(list.isEmpty()){
                    Flag = true;
                }
                else{
                    System.out.println(list.get(0).toJSONObject().toString());
                    Flag = false;
                }
            }
        });

        if(Flag) {
            AVObject todo = new AVObject("ManagerUser");
            todo.put("UserName", UserNameRegister.getText().toString());
            todo.put("PassWord", PassWordRegister.getText().toString());
            todo.put("Mobile", MobileRegister.getText().toString());
            todo.put("RealName", RealNameRegister.getText().toString());
            todo.put("Sure",0);
            todo.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                    if (e == null) {
                        dialog1();
                    }
                }
            });
        }
        else{
            UserNameRegister.setFocusable(true);
            Error.setText("该用户名已经被注册！");
        }

    }

    /**
     * 注册成功对话框
     */
    private void dialog1(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("注册信息");
        builder.setMessage("注册成功！");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setPositiveButton("返回主界面", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); //关闭dialog
                Intent intent = new Intent();
                intent.setClass(RegisterPage.this,LoginPage.class);
                startActivity(intent);
            }
        });

        builder.create().show();
    }



}
